package com.patMall.service.impl;

import com.patMall.entity.*;
import com.patMall.mapper.*;
import com.patMall.service.ConcernService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.service.impl
 * @Date 2024/2/17 16:45
 * @Version 1.0
 */
@Service
public class ConcernServiceImpl implements ConcernService {
    @Resource
    private ConcernMapper concernMapper;
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private CommodityImgMapper commodityImgMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public int addConcern(Concern concern) {
        return concernMapper.insertConcern(concern);
    }

    @Override
    public int updateConcern(Concern concern) {
        return concernMapper.updateConcern(concern);
    }

    @Override
    public List<Concern> getConcern(Integer userId) {
        return concernMapper.selectConcern(userId);
    }

    @Override
    public List<Concern> getConcernUser(Integer userId) {
        List<Concern> concernList = concernMapper.selectConcern(userId);
        for (Concern concern : concernList) {
            if (concern.getCommodityId() != null) {
                Commodity commodity = commodityMapper.getCommodityId(concern.getCommodityId());
                List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
                commodity.setCommodityImgList(commodityImg);
                concern.setCommodity(commodity);
            }
            User user = userMapper.selectByPrimaryKey(concern.getUserId());
            concern.setUser(user);
                Store store = storeMapper.selectByStoreId(concern.getStoreId());
                concern.setStore(store);
        }
        return concernList;
    }

    @Override
    public List<Concern> findConcern(String role) {
        List<Concern> concernList = concernMapper.findConcern(role);
        for (Concern concern : concernList) {
            if (concern.getCommodityId() != null) {
                Commodity commodity = commodityMapper.getCommodityId(concern.getCommodityId());
                List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
                commodity.setCommodityImgList(commodityImg);
                concern.setCommodity(commodity);
            }
            User user = userMapper.selectByPrimaryKey(concern.getUserId());
            concern.setUser(user);
                Store store = storeMapper.selectByStoreId(concern.getStoreId());
                concern.setStore(store);
        }
        return concernList;
    }

    @Override
    public List<Concern> findConcernUser(String username) {
        User user = userMapper.selectUsername(username);
        List<Concern> concernList = concernMapper.selectConcernUserId(user.getUserId());
            for (Concern concern : concernList) {
                if (concern.getCommodityId() != null) {
                    Commodity commodity = commodityMapper.getCommodityId(concern.getCommodityId());
                    List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
                    commodity.setCommodityImgList(commodityImg);
                    concern.setCommodity(commodity);
                }
                User user1 = userMapper.selectByPrimaryKey(concern.getUserId());
                concern.setUser(user1);
                Store store = storeMapper.selectByStoreId(concern.getStoreId());
                concern.setStore(store);
            }
        return concernList;
    }
}
